/*
 * @Author: gaojinyu 1593026814@qq.com
 * @Date: 2023-12-07 14:24:09
 * @LastEditors: gaojinyu 1593026814@qq.com
 * @LastEditTime: 2024-01-09 17:10:03
 * @FilePath: \test\src\hooks\useAMapWeather.js
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
import { ref, onMounted, onUnmounted, getCurrentInstance } from "vue";
let _this = getCurrentInstance().appContext.config.globalProperties;
// dayjs
import dayjs from "dayjs";

export function useAMapWeather() {
  // 当前实时时间
  let nowTimes = ref("");
  let timer = null;

  onMounted(() => {
    timer = setInterval(() => {
      // dayjs格式化
      nowTimes.value = dayjs().format("YYYY-MM-DD HH:mm:ss");
    }, 1000);

    _this.$AMapLoader
      .load({
        key: "9900805441108295c4be4ba65c6b33cb", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ["AMap.Weather"], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
      })
      .then((AMap) => {
        console.log(AMap, "Amap");
        //创建天气查询实例
        var weather = new AMap.Weather();

        //执行实时天气信息查询
        weather.getLive("沈阳市", function (err, data) {
          console.log(err, data, "杭州市");
        });
      })
      .catch((e) => {
        console.log(e);
      });
  });

  onUnmounted(() => {
    clearInterval(timer);
  });

  return { nowTimes };
}
